﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;

namespace DotNet.Common
{
    public class XMLUtil
    {
        public static string ReturnTargetText(string ProType, string TName)
        {
            return ReturnTargetText(CommUtil.GetAppPath(1), DotConfig.GetAppSettingsValue("language"), ProType, TName);
        }
        #region 翻译配置
        /// <summary>
        /// 得到相应的翻译
        /// </summary>
        /// <param name="LocalUrl">网站根路径</param>
        /// <param name="Language">语言</param>
        /// <param name="ProType">类型分块（Page：页面；Js：脚本）</param>
        /// <param name="TName">翻译原文</param>
        /// <returns></returns>
        public static string ReturnTargetText(string LocalUrl, string Language, string ProType, string TName)
        {
            string TitleName = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(LocalUrl + "/language/xml/" + Language + ".xml");
            XmlNodeList xmlnode = xmldoc.SelectSingleNode("root").ChildNodes;

            for (int i = 0; i < xmlnode.Count; i++)
            {
                if (xmlnode.Item(i).Name == ProType)
                {
                    xmlnode = xmlnode.Item(i).ChildNodes;
                    for (int j = 0; j < xmlnode.Count; j++)
                    {

                        if (xmlnode.Item(j).Name == "lg" && xmlnode.Item(j).Attributes[0].Value == TName)
                        {
                            TitleName = xmlnode.Item(j).Attributes["TargetText"].Value;
                        }
                    }
                }
            }
            return TitleName;
        }
        /// <summary>
        /// 得到相应的原文
        /// </summary>
        /// <param name="Language">网站根路径</param>
        /// <param name="Language">语言</param>
        /// <param name="ProType">类型分块（Page：页面；Js：脚本）</param>
        /// <param name="TName">翻译</param>
        /// <returns></returns>
        public static string ReturnSourceText(string LocalUrl, string Language, string ProType, string TName)
        {
            string TitleName = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(LocalUrl + "/language/xml/" + Language + ".xml");
            XmlNodeList xmlnode = xmldoc.SelectSingleNode("root").ChildNodes;

            for (int i = 0; i < xmlnode.Count; i++)
            {
                if (xmlnode.Item(i).Name == ProType)
                {
                    xmlnode = xmlnode.Item(i).ChildNodes;
                    for (int j = 0; j < xmlnode.Count; j++)
                    {
                        if (xmlnode.Item(j).Name == "lg" && xmlnode.Item(j).Attributes[0].Value == TName)
                        {
                            TitleName = xmlnode.Item(j).Attributes["SourceText"].Value;
                        }
                    }
                }
            }
            return TitleName;
        }
        /// <summary>
        /// 以域名返回语言
        /// </summary>
        /// <param name="LocalUrl">网站根路径</param>
        /// <param name="domain">域名（PS：www.aaa.com或者cn.aaa.com不接收aaa.com）</param>
        /// <returns></returns>
        public static string ReturnDomainLG(string LocalUrl, string domain)
        {
            string LG = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(LocalUrl + "/language/xml/domain.xml");
            XmlNodeList xmlnode = xmldoc.SelectSingleNode("root").ChildNodes;

            for (int i = 0; i < xmlnode.Count; i++)
            {
                if (xmlnode.Item(i).Name == "lg" && xmlnode.Item(i).Attributes["HostName"].Value == domain)
                    LG = xmlnode[i].Attributes["Language"].Value;
            }
            return LG;
        }
        /// <summary>
        /// 以域名返回选择的模板
        /// </summary>
        /// <param name="LocalUrl">网站根路径</param>
        /// <param name="domain">域名（PS：www.aaa.com或者cn.aaa.com不接收aaa.com）</param>
        /// <returns></returns>
        public static string ReturnDomainModel(string LocalUrl, string domain)
        {
            string Model = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(LocalUrl + "/language/xml/domain.xml");
            XmlNodeList xmlnode = xmldoc.SelectSingleNode("root").ChildNodes;

            for (int i = 0; i < xmlnode.Count; i++)
            {
                if (xmlnode.Item(i).Name == "lg" && xmlnode.Item(i).Attributes["HostName"].Value == domain)
                    Model = xmlnode[i].Attributes["SelectModel"].Value;
            }
            return Model;
        }
        /// <summary>
        /// 以语言返回域名
        /// </summary>
        /// <param name="LocalUrl">网站根路径</param>
        /// <param name="LG">语言（PS：en）</param>
        /// <returns></returns>
        public static string ReturnLGDomain(string LocalUrl, string LG)
        {
            string Domain = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(LocalUrl + "/language/xml/domain.xml");
            XmlNodeList xmlnode = xmldoc.SelectSingleNode("root").ChildNodes;

            for (int i = 0; i < xmlnode.Count; i++)
            {
                if (xmlnode.Item(i).Name == "lg" && xmlnode.Item(i).Attributes["Language"].Value == LG)
                    Domain = xmlnode[i].Attributes["HostName"].Value;
            }
            return Domain;
        }
        #endregion
    }
}
